package com.jz.community.moduleshopping.confirmOrder.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.commview.callback.EmptyCallback;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.confirmOrder.adapter.ShopCouponListAdapter;
import com.jz.community.moduleshopping.confirmOrder.bean.BaseOrderCouponInfo;
import com.jz.community.moduleshopping.confirmOrder.bean.OrderCouponInfo;
import com.jz.community.moduleshopping.confirmOrder.util.OrderRequestConstant;
import com.jz.community.moduleshopping.goodsDetail.adapter.GoodsCouponAdapter;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.kingja.loadsir.core.Transport;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/5/30 08:59
 * 订单优惠卷
 */
public class OrderCouponActivity extends BaseMvpActivity {
    @BindView(R2.id.od_coupon_back)
    ImageView odCouponBack;
    @BindView(R2.id.od_coupon_title)
    TextView odCouponTitle;
    @BindView(R2.id.od_coupon_toolbar)
    Toolbar odCouponToolbar;
    @BindView(R2.id.od_coupon_rv)
    RecyclerView odCouponRv;
    @BindView(R2.id.od_coupon_btn)
    RelativeLayout odCouponBtn;
    private List<BaseOrderCouponInfo> platformCoupons;
    private List<OrderCouponInfo.MerchantCouponsBean> merchantCoupons;
    private LoadService loadService;
    private GoodsCouponAdapter goodsCouponAdapter;
    private int couponFrom;
    private int position,couponPosition;
    private String platformCouponId;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_activity_od_coupon;
    }

    @Override
    protected void initView() {
        setImmersionBar(odCouponToolbar);
        odCouponTitle.setText("选择优惠券");
        loadService = LoadSir.getDefault().register(odCouponRv);

        loadService.setCallBack(EmptyCallback.class, new Transport() {
            @Override
            public void order(Context context, View view) {
                ImageView mIvEmpty = view.findViewById(R.id.empty_img);
                TextView mTvEmpty = view.findViewById(R.id.empty_tv);
                mIvEmpty.setImageResource(R.mipmap.empty_default);
                mTvEmpty.setText("暂无优惠券！");
                SHelper.gone(odCouponBtn);
            }
        });
    }

    @Override
    protected void initDatas() {
        Bundle bundle = getIntent().getExtras();
        couponFrom = bundle.getInt("couponFrom");
        position = bundle.getInt("position");
        couponPosition = bundle.getInt("couponPosition");
        platformCouponId = bundle.getString("platformCouponId");
        handleShopCouponFrom(bundle);
        super.initDatas();
    }

    private void handleShopCouponFrom(Bundle bundle) {
        switch (couponFrom) {
            case 0:
                platformCoupons = JsonUtils.parseArray(bundle.getString("jsonCouponInfo"), BaseOrderCouponInfo.class);
                if (!Preconditions.isNullOrEmpty(platformCoupons)) {
                    SHelper.vis(odCouponBtn);
                    loadService.showSuccess();
                    handleBindPlatformCouponAdapter(platformCoupons);
                } else {
                    loadService.showCallback(EmptyCallback.class);
                }
                break;
            case 1:
                merchantCoupons = JsonUtils.parseArray(bundle.getString("jsonCouponInfo"), OrderCouponInfo.MerchantCouponsBean.class);
                if (!Preconditions.isNullOrEmpty(merchantCoupons)) {
                    SHelper.vis(odCouponBtn);
                    loadService.showSuccess();
                    handleBindShopCouponAdapter(merchantCoupons);
                } else {
                    loadService.showCallback(EmptyCallback.class);
                }
                break;
        }
    }

    /**
     * 平台卷
     *
     * @param contentBeans
     */
    private void handleBindPlatformCouponAdapter(List<BaseOrderCouponInfo> contentBeans) {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        odCouponRv.setLayoutManager(main_layoutManager);
        goodsCouponAdapter = new GoodsCouponAdapter(contentBeans, platformCouponId);
        goodsCouponAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        odCouponRv.setAdapter(goodsCouponAdapter);

        goodsCouponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setResultCouponData(position, goodsCouponAdapter);
            }
        });
        goodsCouponAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.coupon_gt_click_tv) {
                    setResultCouponData(position, goodsCouponAdapter);
                }
            }
        });
    }

    /**
     * 店铺卷
     *
     * @param contentBeans
     */
    private void handleBindShopCouponAdapter(List<OrderCouponInfo.MerchantCouponsBean> contentBeans) {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        odCouponRv.setLayoutManager(main_layoutManager);
        goodsCouponAdapter = new GoodsCouponAdapter(contentBeans.get(position).getCouponList(), platformCouponId);
        goodsCouponAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        odCouponRv.setAdapter(goodsCouponAdapter);

        goodsCouponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setResultCouponData(position, goodsCouponAdapter);
            }
        });
        goodsCouponAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.coupon_gt_click_tv) {
                    setResultCouponData(position, goodsCouponAdapter);
                }
            }
        });
    }

    /**
     * 设置优惠数据
     *
     * @param position
     */
    private void setResultCouponData(int position, GoodsCouponAdapter goodsCouponAdapter) {
        Intent intent = new Intent();
        intent.putExtra("couponId", goodsCouponAdapter.getData().get(position).getId());
        intent.putExtra("couponType", goodsCouponAdapter.getData().get(position).getCouponType());
        String couponAmount = goodsCouponAdapter.getData().get(position).getCouponAmount();
        intent.putExtra("couponAmount", Preconditions.isNullOrEmpty(couponAmount) ? "" : couponAmount);
        String couponDiscount = goodsCouponAdapter.getData().get(position).getCouponDiscount();
        intent.putExtra("couponDiscount", Preconditions.isNullOrEmpty(couponDiscount) ? "" : couponDiscount);
        intent.putExtra("subMoney", goodsCouponAdapter.getData().get(position).getSubMoney());
        intent.putExtra("couponName", goodsCouponAdapter.getData().get(position).getCouponName());
        intent.putExtra("couponPosition", position);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void addListener() {
        super.addListener();
        odCouponBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        odCouponBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("couponName", "");
                intent.putExtra("couponId", "");
                intent.putExtra("subMoney", "");
                intent.putExtra("couponPosition", couponPosition);
                setResult(OrderRequestConstant.REQUEST_COUPON_CANCEL_RESULT_CODE, intent);
                finish();
            }
        });
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

}
